package servletTestTwo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servletTestTwo.entity.Entity;
import servletTestTwo.service.ServiceOne;
import servletTestTwo.service.impl.ServiceImpl;

public class Servlet extends HttpServlet {
	static ServiceOne serviceOne = new ServiceImpl();
	static List<Entity> entityList = new ArrayList<>();
	private Long count = 1L;
	private static String user;
	private long id;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		System.out.println(method);
		if ("add".equals(method)) {
			add(req, response);
			list(response);
		} else if ("update".equals(method)) {
			update(req, response);
			list(response);
		} else if ("delete".equals(method)) {
			delete(req);
			list(response);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, response);
		} else if ("List".equals(method)) {
			list(response);
		} else {
			list(response);
		}
	}

	private void add(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(req.getParameter("user"));
		System.out.println("update");
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		Entity entity = new Entity();
		entity.setPassWord(password);
		entity.setPhone(phone);
		entity.setUserName(userName);
		serviceOne.add(entity);
	}

	private void update(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(req.getParameter("user"));
		System.out.println("update");
		Long id = Long.valueOf(req.getParameter("id"));
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		Entity entity = new Entity();
		entity.setId(id);
		entity.setPassWord(password);
		entity.setPhone(phone);
		entity.setUserName(userName);
		serviceOne.update(entity);
	}

	private void toUpdate(ServletRequest req, ServletResponse response) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		//Ctrl +Alt +R 重命名快捷键
		Entity entity = serviceOne.getById(id);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<form action=\"LeiYuanDong?method=update\" method=\"post\">");
		out.println("<input type='hidden' name='id' value='" + entity.getId() + "'><p>");
		out.println("<input name='userName' value='" + entity.getUserName() + "'><p>");
		out.println("<input name='phone' value='" + entity.getPhone() + "'>");
		out.println("<input name='password' value='" + entity.getPassWord() + "'>");
		out.println("<input name='creationTime' value='" + entity.getCreationTime() + "'>");
		out.println("<input name='updateTime'value='" + entity.getUpdateTime() + "'>");
		out.println("<input type=\"submit\" value=\"提交\">\r\n" + "</form>");
	}

	private void delete(ServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
//		entityList.forEach(e -> {
//			if (e.getId().equals(id)) {
//				e.setDeleteLogo("Y");
//			}
//		});
		serviceOne.delete(id);
	}

	private void list(ServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>" + user + "</h1>");
		out.println("<a href='test/testTwo.html'>添加</a>");
		out.println("<table border=1>");
		out.print("<tr><td>id</td><td>姓名</td><td>电话</td><td>密码</td><td>创建时间</td><td>修改时间</td><tr>");
		List<Entity> userList = serviceOne.listAll();
		for (Entity us : userList) {
			if ("Y".equals(us.getDeleteLogo())) {
				continue;
			}
			out.println("<tr>");
			out.println("<td>");
			out.print(us.getUserName());
			out.print("</td>");
			out.print("<td>");
			out.print(us.getPassWord());
			out.print("</td");
			out.print("<td>");
			out.print(us.getPhone());
			out.print("</td>");
//			out.print("<td>");
//			out.print(us.creationTime);
//			out.print("</td>");
//			out.print("<td>");
//			out.print(us.getDeleteLogo());
//			out.print("</td>");
//			out.print("<td>");
//			out.print(us.getUpdateTime());
//			out.print("</td>");
			out.print("<td>");
			out.print("<a href='LeiYuanDong?method=delete&id=" + us.getId() + "'>删除</a>    ");
			out.print("<a href='LeiYuanDong?method=toUpdate&id=" + us.getId() + "'>修改</a>");
			out.println("</td>");
			out.print("</tr>");
		}
		out.println("</table>");
	}

	@Override
	public void destroy() {
		System.out.println("servlet销毁...");
		super.destroy();
	}

}
